import 'package:flui/flui.dart';

import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:fzcgx_app/common/adapter_utils.dart';
import 'package:fzcgx_app/common/assets_utils.dart';
import 'package:fzcgx_app/ui/home/model/home_order_model.dart';
import 'package:fzcgx_app/ui/home/view_model/home_view_model.dart';
import 'package:fzcgx_app/ui/order/page/order_detail_page.dart';
import 'package:nav_router/nav_router.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class HomeOrderPage extends StatefulWidget {
  final HomeViewModel viewModel;
  HomeOrderPage({Key key, this.viewModel}) : super(key: key);
  @override
  _HomeOrderPageState createState() => _HomeOrderPageState();
}

class _HomeOrderPageState extends State<HomeOrderPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SmartRefresher(
                controller: widget.viewModel.refreshController,
                enablePullDown: true,
                enablePullUp: false,
                onRefresh: widget.viewModel.onRefresh,
                // onLoading: widget.viewModel.onLoading,
                header: WaterDropHeader(
                  waterDropColor: Color(0xff15A1FF),
                ),
                child: SingleChildScrollView(
      child: Container(
        margin: EdgeInsets.symmetric(horizontal: AdapterUtils.getWidth(15)),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            SizedBox(
              height: AdapterUtils.getHeight(14),
            ),
            Container(
              height: AdapterUtils.getHeight(137),
              child: Swiper(
                itemCount: widget.viewModel.orderModel.orderAdsModels.length,
                autoplay: true,
                pagination: SwiperCustomPagination(
                    builder: (BuildContext context, SwiperPluginConfig config) {
                  return Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: _buildPaginationWidget(config.activeIndex),
                  );
                }),
                itemBuilder: (BuildContext context, int index) {
                  return Image.network(
                    widget.viewModel.orderModel.orderAdsModels[index].img,
                    fit: BoxFit.cover,
                  );
                },
                onIndexChanged: (index) {},
              ),
            ),
            SizedBox(
              height: AdapterUtils.getHeight(20),
            ),
            Row(
              children: <Widget>[
                Container(
                  child: Stack(
                    children: <Widget>[
                      Text(
                        '外发订单',
                        style: TextStyle(
                            color: Color(0xff141F33),
                            fontWeight: FontWeight.bold,
                            fontSize: AdapterUtils.getSp(15)),
                      ),
                      Positioned(
                          bottom: 0,
                          child: Container(
                            color: Color(0xff15A1FF).withAlpha(800),
                            height: AdapterUtils.getHeight(11),
                            width: AdapterUtils.getWidth(60),
                          ))
                    ],
                  ),
                ),
                Spacer(),
                Text('查看更多',
                    style: TextStyle(
                        color: Color(0xff99A2AD),
                        fontWeight: FontWeight.w500,
                        fontSize: AdapterUtils.getSp(12))),
                SizedBox(
                  width: AdapterUtils.getWidth(4),
                ),
                Image.asset(
                    AssetsUtils.getAssetsImagesPath('home_arrow_right.png'))
              ],
            ),
            SizedBox(
              height: AdapterUtils.getHeight(34),
            ),
            Container(
                child: ListView.builder(
                    primary: false,
                    shrinkWrap: true,
                    itemCount: widget.viewModel.orderModel.orderModels.length,
                    itemBuilder: (BuildContext context, int index) {
                      return _buildOrderWidget(
                          widget.viewModel.orderModel.orderModels[index]);
                    }))
          ],
        ),
      ),
    ),
        ),
        );
  }

  Widget _buildOrderWidget(HomeOrderModel homeOrderModel) {
    return InkWell(
      onTap: () {
          routePush( OrderDetailPage(
              id: homeOrderModel.id,
            ));
       
      },
      child: Container(
        child: Column(
          children: <Widget>[
            Container(
              child: Row(
                children: <Widget>[
                  FLAvatar(
                    image: Image.network(homeOrderModel.uhead),
                    width: AdapterUtils.getWidth(36),
                    height: AdapterUtils.getHeight(36),
                  ),
                  SizedBox(width: AdapterUtils.getWidth(10)),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text(homeOrderModel.proName,
                          style: TextStyle(
                              color: Color(0xff141F33),
                              fontWeight: FontWeight.bold,
                              fontSize: AdapterUtils.getSp(14))),
                             
                      Row(
                        children: <Widget>[
                          Text(homeOrderModel.createTime,
                              style: TextStyle(
                                  color: Color(0xff99A2AD),
                                  fontWeight: FontWeight.w500,
                                  fontSize: AdapterUtils.getSp(12))),
                                   SizedBox(width: AdapterUtils.getWidth(10)),
                          Container(
                            constraints: BoxConstraints.expand(
                              height: AdapterUtils.getHeight(16),
                              width: AdapterUtils.getWidth(36),
                            ),
                            decoration: BoxDecoration(
                              image: DecorationImage(
                                image: AssetImage(
                                    AssetsUtils.getAssetsImagesPath(
                                        homeOrderModel.tagImagesStr)),
                                //这里是从assets静态文件中获取的，也可以new NetworkImage(）从网络上获取
                                centerSlice: new Rect.fromLTRB(
                                    270.0, 180.0, 1360.0, 730.0),
                              ),
                            ),
                            alignment: Alignment.center,
                            child: Text(homeOrderModel.robTypeStr,
                                style: TextStyle(
                                    color: Colors.white,
                                    fontWeight: FontWeight.w500,
                                    fontSize: AdapterUtils.getSp(8))),
                          )
                        ],
                      )
                    ],
                  ),
                  Spacer(),
                  Text(homeOrderModel.distance,
                      style: TextStyle(
                          color: Color(0xff99A2AD),
                          fontWeight: FontWeight.w500,
                          fontSize: AdapterUtils.getSp(11))),
                ],
              ),
            ),
            SizedBox(
              height: AdapterUtils.getHeight(22),
            ),
            Container(
              margin: EdgeInsets.only(left: AdapterUtils.getWidth(10)),
              child: Row(
                children: <Widget>[
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      
                     
                      RichText(
                        text: TextSpan(children: <InlineSpan>[
                          TextSpan(
                            text: '款式名：',
                            style: TextStyle(
                              fontSize: AdapterUtils.getSp(12),
                              color: Color(0xff99A2AD),
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                          TextSpan(
                            text: homeOrderModel.proStyle,
                            style: TextStyle(
                              fontSize: AdapterUtils.getSp(12),
                              color: Color(0xff606B79),
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                        ]),
                      ),
                      SizedBox(
                        height: AdapterUtils.getHeight(6),
                      ),
                      RichText(
                        text: TextSpan(children: <InlineSpan>[
                          TextSpan(
                            text: '数量（件）：',
                            style: TextStyle(
                              fontSize: AdapterUtils.getSp(12),
                              color: Color(0xff99A2AD),
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                          TextSpan(
                            text: homeOrderModel.number,
                            style: TextStyle(
                              fontSize: AdapterUtils.getSp(12),
                              color: Color(0xff606B79),
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                        ]),
                      ),
                      SizedBox(
                        height: AdapterUtils.getHeight(6),
                      ),
                      RichText(
                        text: TextSpan(children: <InlineSpan>[
                          TextSpan(
                            text: '加工费（元）：',
                            style: TextStyle(
                              fontSize: AdapterUtils.getSp(12),
                              color: Color(0xff99A2AD),
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                          TextSpan(
                            text: homeOrderModel.price,
                            style: TextStyle(
                              fontSize: AdapterUtils.getSp(12),
                              color: Color(0xff606B79),
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                        ]),
                      ),
                      SizedBox(
                        height: AdapterUtils.getHeight(6),
                      ),
                      RichText(
                        text: TextSpan(children: <InlineSpan>[
                          TextSpan(
                            text: '交货期：',
                            style: TextStyle(
                              fontSize: AdapterUtils.getSp(12),
                              color: Color(0xff99A2AD),
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                          TextSpan(
                            text: homeOrderModel.endTime,
                            style: TextStyle(
                              fontSize: AdapterUtils.getSp(12),
                              color: Color(0xffFF3D3E),
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                        ]),
                      ),
                       SizedBox(
                        height: AdapterUtils.getHeight(6),
                      ),
                      RichText(
                        text: TextSpan(children: <InlineSpan>[
                          TextSpan(
                            text: '当前可抢：',
                            style: TextStyle(
                              fontSize: AdapterUtils.getSp(12),
                              color: Color(0xff99A2AD),
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                          TextSpan(
                            text: homeOrderModel.robNum,
                            style: TextStyle(
                              fontSize: AdapterUtils.getSp(12),
                              color: Color(0xffFF3D3E),
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                          TextSpan(
                            text: '/${ homeOrderModel.robTotal}',
                            style: TextStyle(
                              fontSize: AdapterUtils.getSp(12),
                              color: Color(0xff606B79),
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                        ]),
                      )
                    ],
                  ),
                  Spacer(),
                  Container(
                    width: AdapterUtils.getWidth(70),
                    height: AdapterUtils.getHeight(77),
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      color: Color(0xffEAF7FE),
                      borderRadius: BorderRadius.all(Radius.circular(12)),
                    ),
                    child: Text(
                      '抢单',
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        fontSize: AdapterUtils.getSp(13),
                        color: Color(0xff15A1FF),
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                  )
                ],
              ),
            ),
            SizedBox(
              height: AdapterUtils.getHeight(26),
            ),
          ],
        ),
      ),
    );
  }

  List<Widget> _buildPaginationWidget(int index) {
    List<Widget> lists = List<Widget>();
    for (int i = 0;
        i < widget.viewModel.orderModel.orderAdsModels.length;
        i++) {
      lists.add(Align(
          alignment: Alignment.bottomRight,
          child: Container(
            width: AdapterUtils.getWidth(12),
            height: AdapterUtils.getHeight(3),
            margin: EdgeInsets.only(
                right: AdapterUtils.getWidth(12),
                bottom: AdapterUtils.getHeight(12)),
            decoration: BoxDecoration(
                color: index == i ? Color(0xff15A1FF) : Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(1.6))),
          )));
    }
    return lists;
  }
}
